package com.ly.lypopupwindowdemo.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.ly.lypopupwindowdemo.R;
import com.ly.lypopupwindowdemo.adapter.RvAdapter;
import com.ly.lypopupwindowdemo.bean.RvBean;
import com.ly.lypopupwindowdemo.decoration.SimplePaddingDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.OnItemClick;
import butterknife.OnItemLongClick;

import static android.widget.GridLayout.HORIZONTAL;
import static android.widget.GridLayout.VERTICAL;

public class RvActivity extends AppCompatActivity  {

    private List<RvBean> lists = new ArrayList<>();
    private RvAdapter adapter;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_rv);

        initData();
    }

    private void initData() {

        RvBean bean1 = new RvBean("周杰伦0", "稻香");
        RvBean bean2 = new RvBean("周杰伦1", "半岛铁盒");
        RvBean bean3 = new RvBean("周杰伦2", "告白气球");
        RvBean bean4 = new RvBean("周杰伦3", "尤克里里");
        RvBean bean5 = new RvBean("周杰伦4", "双截棍");
        RvBean bean6 = new RvBean("周杰伦5", "晴天");
        RvBean bean7 = new RvBean("周杰伦6", "七里香");
        RvBean bean8 = new RvBean("周杰伦7", "一路向北");
        RvBean bean9 = new RvBean("周杰伦8", "给我一首歌的时间");
        RvBean bean10 = new RvBean("周杰伦9", "开不了口");
        RvBean bean11 = new RvBean("周杰伦10", "说好的幸福呢");
        RvBean bean12 = new RvBean("周杰伦11", "算什么男人");
        RvBean bean13 = new RvBean("周杰伦12", "夜曲");
        RvBean bean14 = new RvBean("周杰伦13", "蒲公英的秘密");
        RvBean bean15 = new RvBean("周杰伦14", "甜甜的");
        RvBean bean16 = new RvBean("周杰伦15", "明明就");

        lists.add(bean1);
        lists.add(bean1);
        lists.add(bean2);
        lists.add(bean3);
        lists.add(bean4);
        lists.add(bean5);
        lists.add(bean6);
        lists.add(bean7);
        lists.add(bean8);
        lists.add(bean9);
        lists.add(bean10);
        lists.add(bean11);
        lists.add(bean12);
        lists.add(bean13);
        lists.add(bean14);
        lists.add(bean15);
        lists.add(bean16);
        lists.add(bean1);
        lists.add(bean1);
        lists.add(bean2);
        lists.add(bean3);
        lists.add(bean4);
        lists.add(bean5);
        lists.add(bean6);
        lists.add(bean7);
        lists.add(bean8);
        lists.add(bean9);
        lists.add(bean10);
        lists.add(bean11);
        lists.add(bean12);
        lists.add(bean13);
        lists.add(bean14);
        lists.add(bean15);
        lists.add(bean16);
        lists.add(bean1);
        lists.add(bean1);
        lists.add(bean2);
        lists.add(bean3);
        lists.add(bean4);
        lists.add(bean5);
        lists.add(bean6);
        lists.add(bean7);
        lists.add(bean8);
        lists.add(bean9);
        lists.add(bean10);
        lists.add(bean11);
        lists.add(bean12);
        lists.add(bean13);
        lists.add(bean14);
        lists.add(bean15);
        lists.add(bean16);


        recyclerView = (RecyclerView) findViewById(R.id.recycleView);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        //manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        manager.setOrientation(LinearLayoutManager.VERTICAL);



        /**
         *
         *  public GridLayoutManager(Context context, int spanCount, int orientation,
         boolean reverseLayout) {
         super(context, orientation, reverseLayout);
         setSpanCount(spanCount);
         }
         *其中第二个参数spanCount表示表格的行数或者列数；
         * 第三个参数表示是水平滑动或者是竖直方向滑动；最后一个参数表示是否从数据的尾部开始显示。
         *
         */
        GridLayoutManager gManager = new GridLayoutManager(this,2,VERTICAL,false);

        ////构造器中，第一个参数表示列数或者行数，第二个参数表示滑动方向
        StaggeredGridLayoutManager  sManager  = new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.VERTICAL);

        adapter = new RvAdapter(lists, this);
        //recyclerView.addItemDecoration(new SimpleDividerDecoration(this));
        //recyclerView.addItemDecoration(new SimplePaddingDecoration(this));
        //recyclerView.addItemDecoration(new LeftAndRightTagDecoration(this));

        //默认分割线

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        /**
         * 需求：实现像listview那样的OnItemClickListener
         * 思路：viewholder可以拿到每个item的跟布局，为这个跟布局设置单独的OnClick监听
         * 并将其开放给adapter。这个listener不是设置到recycleview上的，而是设置到adapter的。
         *
         *
         * 其实也是用到了接口回调的知识
         *
         *
         *
         *
         */


        adapter.setOnItemClickListener(new RvAdapter.MyItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(RvActivity.this, "点击了position=="+position+"content=="+lists.get(position).getContent(), Toast.LENGTH_SHORT).show();
            }
        });


        adapter.setOnItemLongClickListener(new RvAdapter.MyItemLongClickListener() {
            @Override
            public void onItemLongClick(View view, int position) {
                Toast.makeText(RvActivity.this, "长按了position=="+position, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
